//
//  WBTabBar.swift
//  weibo
//
//  Created by 胡古古 on 2017/6/4.
//  Copyright © 2017年 hugugu. All rights reserved.
//

import UIKit

class WBTabBar: UITabBar {
    
    var composeCallBack: (()->())?

    private lazy var composeButton: UIButton = {
        
        let button = UIButton()
        button.setBackgroundImage(UIImage(named:"tabbar_compose_button"), for: .normal)
        button.setBackgroundImage(UIImage(named:"tabbar_compose_button_highlighted"), for: .highlighted)
        
        button.setImage(UIImage(named:"tabbar_compose_icon_add"), for: .normal)
        button.setImage(UIImage(named:"tabbar_compose_icon_add_highlighted"), for: .highlighted)
        
        button.sizeToFit()
        
        button.addTarget(self, action: #selector(clickButton), for: .touchUpInside)
        
        return button
    }()
    
    func clickButton() {
        
        composeCallBack?()
        
    }

    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
       
        super.init(coder: aDecoder)
        
        setupUI()
    }
    
    func setupUI() {
        
        addSubview(composeButton)
        backgroundImage = UIImage(named: "tabbar_background")
        
    }
    
    override func layoutSubviews() {
        
        super.layoutSubviews()
        
        composeButton.center.x = frame.size.width * 0.5
        composeButton.center.y = frame.size.height * 0.5
        
        let buttonWith = frame.size.width * 0.2
        
        var index = 0
        
        for subView in subviews {
            
            if subView.isKind(of: NSClassFromString("UITabBarButton")!) {
                
                subView.frame.size.width = buttonWith
                subView.frame.origin.x = buttonWith * CGFloat(index)
                
                index += 1
                
                if index == 2 {
                    
                    index += 1
                }
                
            }
        }
        
    }
}
